/**
 * @file     findTheDifference.js
 * @brief    [389. 找不同](https://leetcode.cn/problems/find-the-difference/description/)
 * @author   Zhu
 * @date     2023-02-12 23:20
 */

/**
 * @param {string} s
 * @param {string} t
 * @return {character}
 */
var findTheDifference = function (s, t) {
    const tMap = dict(t);
    for (const char of s) {
        tMap[char]--;
    }
    for (const [k, v] of Object.entries(tMap)) {
        if (v != 0) return k;
    }

    function dict(str) {
        const map = {};
        for (const char of str) {
            map[char] = (map[char] ?? 0) + 1;
        }
        return map;
    }
};

/**
 * 异或,拼接成一个字符串，找出出现奇数次的字符
 * @param {string} s
 * @param {string} t
 * @return {character}
 */
var findTheDifference = function (s, t) {
    let res = 0;
    for (const char of s + t) {
        res ^= char.charCodeAt(0);
    }

    return String.fromCharCode(res);
};
